package com.kuang.sync.manageup;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuang.mapper.*;
import com.kuang.pojo.*;
import com.kuang.sync.managedown.ManageDownService;
import com.kuang.utils.MQTTConnect;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@Service
public class ManageUpServiceImpl implements ManageUpService{

    @Resource
    AstManageDeviceRegisterMapper astManageDeviceRegisterMapper;
    @Resource
    AstUserCardActivateMapper astUserCardActivateMapper;
    @Resource
    AstUserCardRechargeMapper astUserCardRechargeMapper;
    @Resource
    AstManageDeviceOpenMapper astManageDeviceOpenMapper;
    @Resource
    AstManageDeviceHeartMapper astManageDeviceHeartMapper;
    @Resource
    ManageDownService manageDownService;
    @Resource
    MQTTConnect mqttConnect;

    @Override
    public String manageDeviceRegisterSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstManageDeviceRegister astManageDeviceRegister = JSON.parseObject(JSON.toJSONString(jsonObject), AstManageDeviceRegister.class);
        //设备编码
        astManageDeviceRegister.setDevice_code(id);
        //主键
        astManageDeviceRegister.setAst_manage_device_register_id(String.valueOf(UUID.randomUUID()));
        astManageDeviceRegister.setCreate_time(new Date());
        astManageDeviceRegister.setCmd(cmd);
        astManageDeviceRegister.setVer(ver);

        int insert = astManageDeviceRegisterMapper.insert(astManageDeviceRegister);

        //这个地方不是实时的需要回馈给平台，平台返回给我数据我再去发送给这个主题
        try {
            String s = manageDownService.postManageDeviceRegisterSrv(id, preant);
        } catch (MqttException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String userCardActivateSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstUserCardActivate astUserCardActivate = JSON.parseObject(JSON.toJSONString(jsonObject), AstUserCardActivate.class);
        ;
        //设备编码
        astUserCardActivate.setDevice_code(id);
        //主键
        astUserCardActivate.setAst_user_card_activate_id(String.valueOf(UUID.randomUUID()));
        astUserCardActivate.setCreate_time(new Date());
        astUserCardActivate.setCmd(cmd);
        astUserCardActivate.setVer(ver);

        int insert = astUserCardActivateMapper.insert(astUserCardActivate);
        return null;
    }

    @Override
    public String userCardRechargeSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstUserCardRecharge astUserCardRecharge = JSON.parseObject(JSON.toJSONString(jsonObject), AstUserCardRecharge.class);
        //设备编码
        astUserCardRecharge.setDevice_code(id);
        //主键
        astUserCardRecharge.setAst_user_card_recharge_id(String.valueOf(UUID.randomUUID()));
        astUserCardRecharge.setCreate_time(new Date());
        astUserCardRecharge.setCmd(cmd);
        astUserCardRecharge.setVer(ver);

        int insert = astUserCardRechargeMapper.insert(astUserCardRecharge);
        return null;
    }

    @Override
    public String manageDeviceOpenSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstManageDeviceOpen astManageDeviceOpen = JSON.parseObject(JSON.toJSONString(jsonObject), AstManageDeviceOpen.class);
        //设备编码
        astManageDeviceOpen.setDevice_code(id);
        //主键
        astManageDeviceOpen.setAst_manage_device_open_id(String.valueOf(UUID.randomUUID()));
        astManageDeviceOpen.setCreate_time(new Date());
        astManageDeviceOpen.setCmd(cmd);
        astManageDeviceOpen.setVer(ver);

        int insert = astManageDeviceOpenMapper.insert(astManageDeviceOpen);

        JSONObject toJson = new JSONObject();
        JSONObject data = new JSONObject();
        toJson.put("cmd",cmd);
        toJson.put("ver",ver);
        data.put("state","1");
        toJson.put("data",data);

        try {
            boolean pub = mqttConnect.pub("/manage/down/"+id, JSON.toJSONString(toJson));//发布主题
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String manageDeviceHeartSrv(String id, String json) {
        JSONObject preant = JSON.parseObject(json);
        String cmd = preant.getString("cmd");
        String ver = preant.getString("ver");
        JSONObject jsonObject = preant.getJSONObject("data");

        AstManageDeviceHeart astManageDeviceHeart = JSON.parseObject(JSON.toJSONString(jsonObject), AstManageDeviceHeart.class);
        //设备编码
        astManageDeviceHeart.setDevice_code(id);
        //主键
        astManageDeviceHeart.setAst_manage_device_heart_id(String.valueOf(UUID.randomUUID()));
        astManageDeviceHeart.setCreate_time(new Date());
        astManageDeviceHeart.setCmd(cmd);
        astManageDeviceHeart.setVer(ver);

        int insert = astManageDeviceHeartMapper.insert(astManageDeviceHeart);

        JSONObject toJson = new JSONObject();
        JSONObject data = new JSONObject();
        toJson.put("cmd",cmd);
        toJson.put("ver",ver);
        data.put("state","1");
        toJson.put("data",data);

        try {
            boolean pub = mqttConnect.pub("/manage/down/"+id, JSON.toJSONString(toJson));//发布主题
        } catch (MqttException e) {
            e.printStackTrace();
        }

        return null;
    }
}
